# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libebml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.10
pkgrel=1
pkgdesc="Extensible Binary Meta Language library (mingw-w64)"
arch=('any')
license=('LGPL')
url="https://matroska.org"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('staticlibs')
source=("https://dl.matroska.org/downloads/${_realname}/${_realname}-${pkgver}.tar.xz"
        002-mingw-versioned-dll.patch)
sha256sums=('c6a792f4127b8059bc446fb32391e6633811f45748d7d7ba873d7028362f5e3e'
            'a7fa1bc8d43acd6dc0fd7665a277d145227c8c729b3b0362a5f8977101ecf539')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-mingw-versioned-dll.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/shared-${MINGW_CHOST} && cd ${srcdir}/shared-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/shared-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.LGPL ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.LGPL
}
